// pages/address/address.js
let utils = require("../../utils/utils.js");
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		addresslist:[],
		noaddres:false
		,figureImg:'../../assets/nav_img/figure.png'
		,codeInfo:{count:'',cName:'',canselTime:60,orderTo:true,codeImg:'/images/code.png'}
		,cancelcancel: '/order/cancel'
		,qrcodeImgSrc:'',
		canselTime:'',
		cardBglastImg: '/code-bg.png'
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let that = this;
		//that.addresslist();

	},

	/**
	 * 生命周期函数--监听页面显示 
	 */
	onShow: function() {
		let that=this;
		let fetchCode=wx.getStorageSync('fetchCodeInfo');
		that.data.canselTime=setInterval(function () {
			that.data.codeInfo.canselTime--;
			that.setData({
				codeInfo:that.data.codeInfo
			});
			if(that.data.codeInfo.canselTime<=0){
				clearInterval(that.data.canselTime);
				wx.redirectTo({
					url:'../myorder/myorder'
				});
			};
		},1000);
		that.data.codeInfo.count=fetchCode.count;
		that.data.codeInfo.cName=fetchCode.cName;
		that.data.codeInfo.takeCode=fetchCode.takeCode;
		that.data.qrcodeImgSrc=app.globalData.baseurl+'/code/qrcode?data='+fetchCode.code;
		this.setData({
			qrcodeImgSrc:that.data.qrcodeImgSrc,
			codeInfo:that.data.codeInfo
		});
		this.img();
	},
	img() {
		let cardBgImg = app.globalData.baseimg + this.data.cardBglastImg;
		this.setData({
			cardBgImg,
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
			
			
	},
	backOrder(){
		wx.redirectTo({
			url:'../myorder/myorder'
		})
	},
	cancelOrder:function () {
		let that = this;
		let fetchCode=wx.getStorageSync('fetchCodeInfo');
		utils.confirm('确认要取消订单吗？',()=> {
			let _data = {url: that.data.cancelcancel, data: {'orderNumber':fetchCode.orderNumber},methods:'post',nonelist:true};
			utils.request.call(_data, (res) => {
				if(res.data.status==200){
					utils.tips('您已取消此订单！');
					setTimeout(function () {
						wx.reLaunch({
							url:'../myorder/myorder'
						});
					},1000);
				}else{
					utils.tips(res.data.msg);
					setTimeout(function () {
						wx.reLaunch({
							url:'../myorder/myorder'
						});
					},1000);
				};
			});
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
		console.log('onhide');
		clearInterval(this.data.canselTime);
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		console.log('onUnload');
		clearInterval(this.data.canselTime);
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
